﻿<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#-- Freemark的包含指令 -->
    <#include "../common/header.ftl"/>
        <#--引入富文本编辑器-->
    <script src="/js/ckeditor/ckeditor.js"></script>
<script>
   $(function () {
       //引入富文本ckeditor编辑器
       var ck = CKEDITOR.replace( 'editor' );

       //给修改按钮绑定点击事件
        $(".showStrategyDetailBtn").click(function () {

            //清空模态框
            $("#editForm input").val("");
            $("#editForm select").val("");
            $("#editForm img[id='coverUrl']").prop("src","");
            //编辑回显
            var json = $(this).data("json");
            if (json) {
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#editForm input[name='coverUrl']").val(json.coverUrl);
                $("#editForm img[id='coverUrl']").prop("src", json.coverUrl);
                $("#editForm input[name='sequence']").val(json.sequence);
                $("#editForm select[name='strategyCatalog.strategy.id']").val(json.strategyId);
                $("#editForm select[name='state']").val(json.state);
                ck.setData(json.content);//回显文本编辑器
                //获取攻略分类下拉框
                $.get("/strategyDetail/listByStrategyId.do?strategyId=" + json.strategyId, function (data) {
                    var temp = "";
                    //循环拼接option
                    $.each(data, function (index, ele) {
                        temp += '<option value="' + ele.id + '">' + ele.name + '</option>';
                    })
                    //攻略分类下拉框
                    $("#catalogSelect").html(temp);
                    //回显攻略分类下拉框
                    $("#editForm select[name='strategyCatalog.id']").val(json.strategyCatalogId);
                })
            }else{
                ck.setData(json.content);//添加的时候清空文本编辑器
            }
            //触发模态框
            $("#inputModal").modal('show');
            //二级联动
            $("#strategySelect").change(function () {
                var strategyId = $(this).val();
                $.get("/strategyDetail/listByStrategyId.do?strategyId="+strategyId,function (data) {
                    var temp = "";
                    //循环拼接option
                    $.each(data,function (index, ele) {
                        temp += '<option value="'+ele.id+'">'+ele.name+'</option>';
                    })
                    //攻略分类下拉框
                    $("#catalogSelect").html(temp);
                })
            })
        });
        //给模态框保存按钮绑定点击事件,提交表单
       $(".modalBtn").click(function () {
           $("#editor").val(ck.getData());
           //发送ajax请求,提交表单
           //将表单的提交方式修改为ajax异步提交
           $("#editForm").ajaxSubmit(function (data) {
               if(data.success){
                   $.messager.alert("温馨提示","操作成功,1.5S之后关闭");
                   setTimeout(function () {
                       window.location.reload();
                   },1500);
               }
           })
       });

   })
</script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#--欢迎-->
    <#include "../common/top.ftl"/>

    <div class="row">
        <div class="col-sm-3">
            <#-- Freemark设置共享变量的指令 -->
            <#assign menu="strategyDetail"/>
            <#--左侧菜单-->
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <#-- 高级查询 -->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                    placeholder="请输入标题">
                </div>
                <input type="submit" id="btn_query" class="btn btn-default" value="查询">
                <a class="btn btn-success showStrategyDetailBtn">
                    <span class="glyphicon glyphicon-pencil" ></span>添加
                </a>
            </form>
            <table class="table table-striped table-hover" >
                <thead>
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                </tr>
                </thead>
            <#-- Freemark指令做循环迭代 -->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${(entity.title)!}</td>
                        <td><img src="${(entity.coverUrl)!}" width="30px"/></td>
                        <td>${(entity.releaseTime)?string('yyyy-MM-dd HH:mm:ss')}</td>
                        <td>${(entity.sequence)!}</td>
                        <td>${(entity.strategyCatalog.name)!}</td>
                        <td>${(entity.stateName)!}</td>
                        <td>
                            <a class="btn btn-info btn-xs btn-input showStrategyDetailBtn" data-json='${(entity.json)!}'>
                                <span class="glyphicon glyphicon-pencil" ></span>修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>

            <#--分页-->
            <#include "../common/page.ftl" />
        </div>
    </div>
</div>
<#--大攻略模态框-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新增大攻略</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm" enctype="multipart/form-data">
                    <input type="hidden" name="id" >
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="title" name="title" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="coverUrl" width="100%"/>
                            <input type="file" class="form-control" name="file" >
                            <input type="hidden" class="form-control" name="coverUrl" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="strategyCatalog.strategy.id" id="strategySelect">
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="strategyCatalog.id" id="catalogSelect">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">序号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sequence" name="sequence" >
                        </div>
                    </div>
                    <#--富文本编辑器-->
                    <textarea name="strategyContent.content" id="editor" rows="10" cols="80">
                    </textarea>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit modalBtn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
</html>